
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */
package com.hlkj.pay.app.common.ext.tencent;

import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.framework.extension.annotation.Extension;
import com.hlkj.pay.app.common.MessageExtPt;
import com.hlkj.pay.app.common.ext.ChannelSendDTO;
import com.hlkj.pay.app.common.ext.MessageUtil;
import com.hlkj.pay.app.common.ext.CommonExtConstant;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20210111.models.SendStatus;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

/**
 * @author monk@techsun.com
 * @date 2023/04/13 11:58
 */
@Slf4j
@Repository
@RequiredArgsConstructor
@Extension(bizId = CommonExtConstant.CHANNEL_TENCENT)
public class TencentSmsExtPt extends TencentCommon implements MessageExtPt {
    @Override
    public String sendMessage(ChannelSendDTO channelSendDTO) {
        if (channelSendDTO.getTestSendMessage()) {
            log.info("TencentSms 短信通道关闭 返回结果:{}",  JsonUtils.toJsonString(channelSendDTO));
            return null;
        } else {
            // 获取通道的配置信息
            SmsClient smsClient = smsClient(channelSendDTO.getSmsConfig());
            SendSmsRequest sendSmsRequest = smsRequest(channelSendDTO);
            log.info("TencentSms sendSms 请求参数:{}", JsonUtils.toJsonString(channelSendDTO));
            SendSmsResponse sendSmsResponse = null;
            try {
                sendSmsResponse = smsClient.SendSms(sendSmsRequest);
            } catch (TencentCloudSDKException e) {
                e.printStackTrace();
                log.error("发送腾讯消息失败:{}", e.getMessage(),e);
                return e.getMessage();
            }
            log.info("TencentSms sendSms 返回结果:{}",  JsonUtils.toJsonString(sendSmsResponse));
            SendStatus[] sendStatusSet = sendSmsResponse.getSendStatusSet();
            if (sendStatusSet.length <= 0) {
                return "腾讯短信发送失败";
            }
            return null;
        }
    }
    /**
     * sms 请求参数
     * @param channelSendDTO
     * @return
     */
    SendSmsRequest smsRequest(ChannelSendDTO channelSendDTO) {
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        String[] phoneNumberSet=new String[1];
        phoneNumberSet[0]=channelSendDTO.getReceiver();
        sendSmsRequest.setPhoneNumberSet(phoneNumberSet);
        sendSmsRequest.setSmsSdkAppId(channelSendDTO.getSmsConfig().getAppId());
        sendSmsRequest.setTemplateId(channelSendDTO.getTemplateDO().getTemplateId());
        sendSmsRequest.setSignName(channelSendDTO.getSmsConfig().getSign());
        
        sendSmsRequest.setTemplateParamSet(
                MessageUtil.templates(channelSendDTO.getTemplateDO().getContent(), channelSendDTO.getParams()));
        // sendSmsRequest.setExtendCode("");
        sendSmsRequest.setSessionContext(channelSendDTO.getMessageRequestId());
        sendSmsRequest.setSenderId("");
        return sendSmsRequest;
    }
    
   
    
}
